package com.qf.controller;

import com.qf.annotation.NoLogin;
import com.qf.entity.user.User;
import com.qf.param.LoginParam;
import com.qf.param.RegisterParam;
import com.qf.param.UserParam;
import com.qf.service.UserService;
import com.qf.util.R;
import io.minio.MinioClient;
import org.hibernate.validator.internal.constraintvalidators.hv.NormalizedValidator;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * --- 万物归集  不随你我 ---
 * <p>
 * 快捷短语
 * main ==  public static void main(String[] args);
 * sout ==  System.out.println();
 *
 * @author shuhan
 * @date 2025/6/9
 * @desc
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    // 登录
    @NoLogin
    @PostMapping("/login")
    public R login(@RequestBody LoginParam loginParam) {

        String token = userService.login(loginParam);

        return R.ok(token);
    }

    // 注册
    @NoLogin
    @PostMapping("/register")
    public R register(@RequestBody RegisterParam registerParam){
        userService.register(registerParam);
        return R.ok();
    }

    // 修改用户信息
    @NoLogin
    @PostMapping("/update")
    public R update(@RequestBody User user){
        userService.update(user);
        return R.ok();

    }

    // 修改并上传头像
    @PostMapping("/updateAvatar")
    public R updateAvatar(MultipartFile file){

        String path = userService.updateAvatar(file);

        return R.ok(path);
    }

    // 修改密码
    @PostMapping("/updatePassword")
    public R forgetPassword(@RequestBody UserParam userParam){

        userService.updatePassword(userParam);
        return R.ok();
    }

    // 发送验证码
    @NoLogin
    @PostMapping("/sendCode")
    public R sendCode(String phone){
        userService.createCode(phone);
        return R.ok();

    }

    // 退出登录 重定向到登录页面
    @GetMapping("/logout")
    public R logout(HttpServletRequest request){
        userService.logout(request);
        return R.ok("退出成功");
    }
}
